import React, { lazy, Suspense } from 'react'
import { lazyFix } from './fixLazyLoad'
const modules = import.meta.glob('../pages/**/*.tsx')
export interface RouteObjectObj {
  children?: RouteObjectObj[]
  element?: React.ReactNode
  component?: React.LazyExoticComponent<any>
  path: string
  isKeepAlive?: boolean
}

export interface IRouterInfo {
  name: string
  path: string
  resourcePath: string
  children?: IRouterInfo[]
  cache?: 0 | 1
}

export const handleRouters = (allRouters: IRouterInfo[]): RouteObjectObj[] => {
  return allRouters?.map((item: IRouterInfo) => ({
    component: item.resourcePath ? lazy(() => lazyFix(modules[`${item.resourcePath}`])) : undefined,
    children: item.children && item.children.length > 0 ? handleRouters(item.children) : undefined,
    path: item.path,
    isKeepAlive: !!item.cache,
  }))
}
